package com.gla.giliair.web.admin;

import com.gla.giliair.entity.Seat;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/9/2 10:03
 */
@Api(tags = "动态座位管理模块")
@RestController
@RequestMapping("/api")
public class AdminSeatController extends BaseController {

    @ApiOperation(value = "获取动态座位列表", notes = "根据航班号，获取动态座位列表", produces = "application/json")
    @GetMapping("/admin/seatList")
    @ResponseBody
    public Map<String, Object> findSeatList(@RequestParam("flightId") Long flightId,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "pageSize", defaultValue = "300") Integer pageSize) {
        if (flightId == null) {
            return MapGenerator.getRes(500, "请输入航班号！");
        }
        return seatService.findSeatsByFlightId(flightId, page, pageSize);
    }

    @ApiOperation(value = "管理员选定座位", notes = "管理员选定座位", produces = "application/json")
    @PostMapping("/admin/selectSeat")
    @ResponseBody
    public Map<String, Object> selectSeat(@RequestParam("seatId") Long seatId,
                                          @RequestParam("orderId") Long orderId) {
        if (seatId == null) {
            return MapGenerator.getRes(500, "请输入座位号！");
        }
        if (orderId == null) {
            return MapGenerator.getRes(501, "请输入订单号！");
        }

        Seat seat = new Seat();
        seat.setSeatId(seatId);
        seat.setOrderId(orderId);

        return seatService.selectSeat(seat, 0L);
    }

    @ApiOperation(value = "管理员取消座位", notes = "管理员取消座位", produces = "application/json")
    @PostMapping("/admin/cancelSeat")
    @ResponseBody
    public Map<String, Object> cancelSeat(@RequestParam("orderId") Long orderId) {
        if (orderId == null) {
            return MapGenerator.getRes(500, "请输入订单号！");
        }

        Seat seat = new Seat();
        seat.setOrderId(orderId);

        return seatService.cancelSeat(seat, 0L);
    }

    @ApiOperation(value = "管理员冻结/解冻座位", notes = "管理员冻结/解冻座位，只能冻结未占用的座位", produces = "application/json")
    @PostMapping("/adminX/lockSeat")
    @ResponseBody
    public Map<String, Object> lockSeat(@RequestParam("seatId") Long seatId,
                                        //不填是否冻结，默认冻结
                                        @RequestParam(value = "isLocked", defaultValue = "true") boolean isLocked) {
        if (seatId == null) {
            return MapGenerator.getRes(500, "请输入座位号！");
        }

        Seat seat = new Seat();
        seat.setSeatId(seatId);
        if (isLocked) {
            seat.setSeatState(2);
        } else {
            seat.setSeatState(0);
        }
        return seatService.lockSeat(seat);
    }

}
